package by.grsu.vashchilo.page.administrator;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.vashchilo.app.AirlineWebApplication;
import by.grsu.vashchilo.dao.impl.TripDao;
import by.grsu.vashchilo.entity.Trip;
import by.grsu.vashchilo.page.administrator.ListTripPage;

public class TripEditPage extends WebPage{

	public TripEditPage(final Trip trip) {
		super();
		final Form<Trip> form = new Form<Trip>("form",
				new CompoundPropertyModel<Trip>(trip));
		add(form);

		form.add(new TextField<String>("id"));

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				final TripDao tripDao = new TripDao(
						AirlineWebApplication.DATA_XML_FOLDER);

				if (trip.getId() == null) {

					tripDao.saveNew(trip);
				} else {

					tripDao.update(trip);
				}

				setResponsePage(new ListTripPage());
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new ListTripPage());
			}
		});
	}

}
